package view;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.DriverManager;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * class HibernateXML - responsible on creating hibernate xml  
 */
public class HibernateXML {

	
	static Connection connection;

	/**
	 * create hibernate xml
	 * @param hibernateXmlPath - the path where to create the xml
	 * @param sqlUser - the sql user
	 * @param sqlPassword - the sql password
	 */
	public static void createHibernateXml(String hibernateXmlPath, String sqlUser, String sqlPassword){
		
		try{		
			
			FileWriter fstream = new FileWriter(hibernateXmlPath);
			BufferedWriter out = new BufferedWriter(fstream);
			
	
			
			String hibernateXML= "<?xml version='1.0' encoding='UTF-8'?>\n"+
		    "<!DOCTYPE hibernate-configuration PUBLIC    \"-//Hibernate/Hibernate Configuration DTD 3.0//EN\" \"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd\">\n"+
		    "<hibernate-configuration>\n"+
		      "<session-factory>\n"+
		        "<property name=\"connection.url\">\n"+
		          "jdbc:mysql://localhost:3306/dropboxserver\n"+
		        "</property>\n"+
		        "<property name=\"connection.driver_class\">\n"+
		        "com.mysql.jdbc.Driver\n"+
		        "</property>\n"+
		        "<property name=\"connection.username\">\n"+
		        sqlUser+"\n"+
		       "</property>\n"+
		        "<property name=\"connection.password\">\n"+
		        sqlPassword+"\n"+
		        "</property>\n"+
		        "<!-- Set AutoCommit to true -->\n"+
		        "<property name=\"connection.autocommit\">\n"+
		          "true\n"+
		        "</property>\n"+
		        "<!-- SQL Dialect to use. Dialects are database specific -->\n"+
		        "<property name=\"dialect\">\n"+
		          "org.hibernate.dialect.MySQLDialect\n"+
		        "</property>\n"+
		        "<!-- Mapping files -->\n"+
		        " <mapping resource=\"User.hbm.xml\"/> \n"+ 
		        " <mapping resource=\"DbFile.hbm.xml\"/> \n"+
		        " <mapping resource=\"DbFolder.hbm.xml\"/> \n"+
		        "</session-factory>\n"+
		    "</hibernate-configuration>\n";


			out.write(hibernateXML);
		
			out.close();
		
			
		}catch (Exception e){
			System.err.println("Error: " + e.getMessage());
		}
		
	
		createTables(sqlUser, sqlPassword);
		
	}
	

	/**
	 * create the sql tables for the server
	 * @param sqlUser - the sql user
	 * @param sqlPassword - the sql password
	 */
	private static void createTables(String sqlUser, String sqlPassword) {
		
		try{
		
		   connection = DriverManager.getConnection("jdbc:mysql://localhost:3306",sqlUser,sqlPassword);

		   Statement st = connection.createStatement();
		   
		   //create DB
		   st.executeUpdate("DROP DATABASE IF EXISTS dropboxserver;");
		   
		   st.executeUpdate("CREATE DATABASE dropboxserver;");
		   st.executeUpdate("use dropboxserver;\n");
		   
		   //create folders table
		   st.executeUpdate("DROP TABLE IF EXISTS folders;\n");
		   st.executeUpdate("CREATE TABLE folders(\n"+
						   "record_id numeric PRIMARY KEY,\n"+
						   "user_id char(20),\n"+
						   "folder_id numeric,\n"+
						   "folder_name char(50),\n"+
						   "is_dropbox boolean\n"+
						   ");\n");
		   
		   //create files table
		   st.executeUpdate("DROP TABLE IF EXISTS files;\n");
		   st.executeUpdate("CREATE TABLE files(\n"+
						   "record_id numeric PRIMARY KEY,\n"+
						   "folder_id numeric,\n"+
						   "file_id numeric,"+
						   "file_name char(50)\n"+
						   ");\n");
		   
		   //create users table
		   st.executeUpdate("DROP TABLE IF EXISTS users;\n");
		   st.executeUpdate("CREATE TABLE users(\n"+
						   "user_id char(20) PRIMARY KEY,\n"+
						   "password char(10),\n"+
						   "folder_id numeric"+
						   ");\n");
		
		
		}
	   catch (SQLException s){
	   }
	   catch (Exception e){
			e.printStackTrace();
	   }
		
	}

}
